import { defineStore } from "pinia";
import { initCasing, updateCasing, changeBlack, changeRed } from "@/api/casing";
import { reactive } from "vue";
import d3DrawStore from "@/store/modules/d3Draw";
const casingStore = defineStore("casingDraw", () => {
  const casing = reactive([]);
  const d3Draw = d3DrawStore();
  const chooseCasing = ref(0);
  const casingData = ref([
    {
      id: 0,
      name: "1",
      outerDiameter: 160.33,
      bottom: 200,
      top: 0,
      innerDiameter: 48.5,
      weight: 0,
      max: 160,
      min: 0,
      alertText: "一开范围 : 0~160",
    },
    {
      id: 1,
      name: "2",
      outerDiameter: 120.26,
      bottom: 1133,
      top: 0,
      innerDiameter: 48.5,
      weight: 0,
      max: 1135,
      min: 160,
      alertText: "二开范围 : 160~1135",
    },
    {
      id: 2,
      name: "3",
      outerDiameter: 68.26,
      bottom: 1500,
      top: 0,
      innerDiameter: 48.5,
      weight: 0,
      max: 1765,
      min: 1135,
      alertText: "三开范围 : 1135~1765",
    },
  ]);
  function casingBlack() {
    for (let i = 0; i < casing.length; i++) {
      changeBlack(casing[i]);
    }
  }
  function casingRed(choose) {
    changeRed(casing[choose]);
  }
  function addCasingData(outer, bottom, top) {
    let obj = {
      id: casingData.value.length,
      name: casingData.value.length + 1,
      outerDiameter: outer,
      bottom: bottom,
      top: top,
      innerDiameter: outer - 10,
      weight: 0,
      max: 2000,
      min: 0,
      alertText: "null",
    };
    casingData.value.push(obj);
  }

  function saveInitCasings(outerDiameter, top, bottom) {
    const $ = d3Draw.$;
    const d3Canvas = d3Draw.d3Canvas;
    casing.push(initCasing($, d3Canvas, outerDiameter, top, bottom));
  }
  function deleteCasing(item) {
    console.log(casing)
    casing.splice(item, 1);
    casingData.value.splice(item, 1);
    for(let i = 0 ; i < casingData.value.length ; i++){
      casingData.value[i].id = i
    }
    console.log(casingData.value)
  }

  return { casingData, casing, casingBlack, saveInitCasings, addCasingData, chooseCasing, casingRed, deleteCasing };
});
export default casingStore;
